package wapi

import (
	"context"
	"ims/app/wechat/apidao/gf_wxfans"

	"github.com/gogf/gf/errors/gerror"
)

var WxfansService wxfans

type wxfans struct{}

func (s *wxfans) AddFans(ctx context.Context, user gf_wxfans.Entity) (interface{}, error) {

	//存入数据库表

	if err := s.OpenidIsExist(user.Openid); err != nil {
		return nil, err
	}

	ret, err := user.OmitEmpty().Save()
	if err != nil {
		return nil, err
	}

	return ret.LastInsertId()
}

func (s *wxfans) OpenidIsExist(openid string) error {
	n, err := gf_wxfans.Model.Where(gf_wxfans.Columns.Openid, openid).Count()
	if err != nil {
		return err
	}
	if n > 0 {
		return gerror.Newf(`昵称"%s"已被占用`, openid)
	}
	return nil

}
